package io.github.smileinmotion.oplog.config;


import io.github.smileinmotion.oplog.aop.OpLogAspect;
import io.github.smileinmotion.oplog.function.CustomFunctionFactory;
import io.github.smileinmotion.oplog.function.OpLogParser;
import io.github.smileinmotion.oplog.function.ICustomFunction;
import io.github.smileinmotion.oplog.function.IFunctionService;
import io.github.smileinmotion.oplog.function.impl.DefaultCustomFunction;
import io.github.smileinmotion.oplog.function.impl.DefaultFunctionServiceImpl;
import io.github.smileinmotion.oplog.service.impl.DefaultOpLogCallServiceImpl;
import io.github.smileinmotion.oplog.util.AppContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Role;

import java.util.List;

/**
 * @author Mr.spc
 */
@Configuration
@ConditionalOnProperty(prefix = "concise.log", name = "enable", havingValue = "true")
@EnableConfigurationProperties({OpLogProperties.class})
@Import({AppContextUtil.class,DefaultOpLogCallServiceImpl.class, OpLogParser.class})
public class OpLogAutoConfiguration {

    @Bean
    public OpLogAspect opAspect() {
        return new OpLogAspect();
    }

    @Bean
    @ConditionalOnMissingBean(ICustomFunction.class)
    @Role(BeanDefinition.ROLE_APPLICATION)
    public ICustomFunction customFunction() {
        return new DefaultCustomFunction();
    }

    @Bean
    public CustomFunctionFactory customFunctionRegistrar(@Autowired List<ICustomFunction> iCustomFunctionList) {
        return new CustomFunctionFactory(iCustomFunctionList);
    }

    @Bean
    public IFunctionService customFunctionService(CustomFunctionFactory customFunctionFactory) {
        return new DefaultFunctionServiceImpl(customFunctionFactory);
    }

}
